#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

void ex_gcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return;
  }
  ex_gcd(b, a % b, y, x);
  y -= (a / b) * x;
}

ll a, b;
int main() {
  cin >> a >> b;
  if (a > b) swap(a, b);
  ll x, y;
  ex_gcd(a, b, x, y);
  if (x > 0) {
    swap(a, b);
    swap(x, y);
  }
  ll tmp = (-x) / b;
  x = x + tmp * b;
  y = y - tmp * a;
  while (x < 0) x = x + b, y = y - a;
  while (x > 0) x = x - b, y = y + a;
  ll ans;
  ll xx2 = x + b;
  ans = a * (xx2 - 1) + b * (y - 1);
  cout << ans - 1 << endl;

  return 0;
}
